﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using uTILLIty.Rave.Core.Manager;
using System.Text;
using System.Collections.Generic;

namespace uTILLIty.Rave.Core
{
	[TestClass]
	public class CsvManagerTest
	{
		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext { get; set; }

		#region Additional test attributes
		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		//[ClassInitialize()]
		//public static void MyClassInitialize(TestContext testContext)
		//{
		//}
		//
		//Use ClassCleanup to run code after all tests in a class have run
		//[ClassCleanup()]
		//public static void MyClassCleanup()
		//{
		//}
		//
		//Use TestInitialize to run code before running each test
		//[TestInitialize()]
		//public void MyTestInitialize()
		//{
		//}
		//
		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//public void MyTestCleanup()
		//{
		//}
		//
		#endregion

		[TestMethod]
		public void SplitCsvLineEnquotedSingleLine()
		{
			var target = new CsvManager();
			const string line = "Some;\"Enquoted;Text\";here";
			const char csvSeparator = ';';
			var expected = new[] { "Some", "Enquoted;Text", "here" };

			string[] actual = target.SplitCsvLine(null, line, csvSeparator);

			Assert.AreEqual(expected.Length, actual.Length);
			for (var i = 0; i < expected.Length; i++)
			{
				Console.WriteLine(actual[i]);
				Assert.AreEqual(expected[i], actual[i]);
			}
		}

		[TestMethod]
		public void SplitCsvLineEnquotedMultiLine()
		{
			var target = new CsvManager();
			var line = "Some;\"Enquoted;Text" + Environment.NewLine + "And some more...\";here";
			const char csvSeparator = ';';
			var expected = new[] { "Some", "Enquoted;Text" + Environment.NewLine + "And some more...", "here" };

			string[] actual = target.SplitCsvLine(null, line, csvSeparator);

			Assert.AreEqual(expected.Length, actual.Length);
			for (var i = 0; i < expected.Length; i++)
			{
				Console.WriteLine(actual[i]);
				Assert.AreEqual(expected[i], actual[i]);
			}
		}

		/// <summary>
		///A test for XmlEscape
		///</summary>
		[TestMethod]
		public void XmlEscapeTest()
		{
			var target = new CsvManager();
			const string value = "P&O";
			var encoding = Encoding.UTF8;
			const string expected = "P&amp;O";

			string actual = target.XmlEscape(value, encoding);

			Console.WriteLine(actual);
			Assert.AreEqual(expected, actual);
		}

		private void PrintKeyValuePairs(Dictionary<string, object> parseValues)
		{
			foreach (var pair in parseValues)
			{
				Console.WriteLine("{0}={1}", pair.Key, pair.Value);
			}
		}
	}
}
